using System;
using System.IO;
using System.Reflection;

namespace HabilisDesign.iTunesCommander
{
	class Program
	{
		private static string m_Command;

		/// <summary>
		/// Mains the specified args.
		/// </summary>
		/// <param name="args">The args.</param>
		static void Main( string[] args )
		{
			DisplayDisclaimer();
			ParseArgs( args );
			if ( !String.IsNullOrEmpty( m_Command ) )
			{
				using ( Controller controller = new Controller() )
				{
					switch ( m_Command )
					{
						case "pause":
							{
								controller.Pause();
								return;
							}
						case "play":
							{
								controller.Play();
								return;
							}
						case "playpause":
							{
								controller.PlayPause();
								return;
							}
						case "quit":
							{
								controller.Quit();
								return;
							}
					}
				}
			}
			DisplayCommands();
		}

		private static void ParseArgs( string[] args )
		{
			if ( args.Length > 0 )
			{
				m_Command = args[0].ToLower();
			}
		}

		/// <summary>
		/// Unwraps the resource.
		/// </summary>
		/// <param name="resourceName">Name of the resource.</param>
		/// <returns></returns>
		private static string UnwrapResource( string resourceName )
		{
			using ( StreamReader reader = new StreamReader( Assembly.GetExecutingAssembly().GetManifestResourceStream( resourceName ) ) )
			{
				return reader.ReadToEnd();
			}
		}

		/// <summary>
		/// Displays the disclaimer.
		/// </summary>
		private static void DisplayDisclaimer()
		{
			string disclaimerFile = "HabilisDesign.iTunesCommander.disclaimer.txt";
			Console.Write( UnwrapResource( disclaimerFile ), Assembly.GetExecutingAssembly().GetName().Version, DateTime.Today.Year );
		}

		/// <summary>
		/// Displays the commands.
		/// </summary>
		private static void DisplayCommands()
		{
			string commandsFile = "HabilisDesign.iTunesCommander.commands.txt";
			Console.Write( UnwrapResource( commandsFile ) );
		}

	} // end class

} // end namespace
